package hello

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

type Cb interface {
	OnMessage(content string)
}

// func main() {
// 	Start()
// }

func Start(path string, handlers Cb) {
	type User struct {
		Name string
	}

	r := gin.Default()

	r.POST(path, func(c *gin.Context) {
		type LoginResult struct {
			Msg    string
			Status string
		}
		type PostReqType struct {
			DeviceId        string
			MeasureDateTime string
			PatientID       string
			SBP             int
			DBP             int
			Pulse           int
			Token           string
		}
		req := PostReqType{}
		err := c.ShouldBind(&req) // 解析req参数
		if err != nil {
			checkResult := LoginResult{Msg: "解析上传参数错误", Status: "FAIL"}
			c.JSON(http.StatusOK, checkResult)
			fmt.Println("ctx.ShouldBindJSON err: ", err)
			return
		}
		bytes, err := json.Marshal(req)
		if err != nil {
			checkResult := LoginResult{Msg: "转换json错误", Status: "FAIL"}
			c.JSON(http.StatusOK, checkResult)
			fmt.Println("ctx.ShouldBindJSON err: ", err)
			return
		}
		handlers.OnMessage(string(bytes))
		fmt.Println(req.SBP)
		fmt.Println(req.DBP)
		fmt.Println(req.Pulse)
		checkResult := LoginResult{Msg: "检查结果上传成功", Status: "SUCCESS"}
		c.JSON(http.StatusOK, checkResult)
	})
	r.Run()
}
